from ckeditor_uploader.fields import RichTextUploadingField
from django.db import models
from django.db.models import Q, F
from django.urls import reverse
from taggit.managers import TaggableManager

from users.models import User
# django-ckeditor
from ckeditor.fields import RichTextField


# Create your models here.
class Plate(models.Model):
    """
    板块主体
    """
    STATUS_NORMAL = 1
    STATUS_HIDDEN = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, "正常"),
        (STATUS_HIDDEN, "隐藏")
    )

    name = models.CharField(max_length=40, unique=True, verbose_name="名称")
    desc = models.CharField(max_length=200, blank=True, null=True, verbose_name="简介")
    #    image = models.ImageField(default=None, null=True, verbose_name="图片")
    status = models.PositiveIntegerField(default=STATUS_NORMAL, choices=STATUS_ITEMS, verbose_name="状态")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    no_message = models.PositiveIntegerField(default=0, verbose_name="留言数")
    no_comment = models.PositiveIntegerField(default=0, verbose_name="评论数")
    weight = models.PositiveIntegerField(default=1, choices=zip(range(1, 8), range(1, 8)),
                                         help_text="权重高者在前", verbose_name="权重")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = verbose_name_plural = "板块"
        ordering = ['-created_time']

    @classmethod
    def get_plates(cls):
        """
        返回各个板块
        :return:
        """
        return cls.objects.filter(status=cls.STATUS_NORMAL).order_by('-weight')


class PlateCategory(models.Model):
    """
    板块中的一些分类
    """
    STATUS_NORMAL = 1
    STATUS_HIDDEN = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, "正常"),
        (STATUS_HIDDEN, "隐藏")
    )
    name = models.CharField(max_length=40, unique=True, verbose_name="分类名称")
    plate = models.ForeignKey(Plate, on_delete=models.CASCADE, verbose_name="所属板块")
    #    image = models.ImageField(null=True, verbose_name="图片")
    status = models.PositiveIntegerField(default=STATUS_NORMAL, choices=STATUS_ITEMS, verbose_name="状态")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = verbose_name_plural = "板块中的分类"
        ordering = ['-created_time']

    @classmethod
    def get_categories(cls):
        return cls.objects.filter(status=cls.STATUS_NORMAL).order_by('-created_time')


class PostColumn(models.Model):
    """
    栏目
    """
    STATUS_NORMAL = 1
    STATUS_HIDDEN = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, "正常"),
        (STATUS_HIDDEN, "隐藏")
    )
    # 栏目的标题
    title = models.CharField(max_length=100, blank=True, verbose_name="栏目标题")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = verbose_name_plural = "文章栏目"
        ordering = ['-created_time']

    def __str__(self):
        return self.title


class PlatePost(models.Model):
    """
    分类下所属的文章
    """
    STATUS_NORMAL = 1
    STATUS_HIDDEN = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, "正常"),
        (STATUS_HIDDEN, "隐藏")
    )
    title = models.CharField(default='', max_length=40, verbose_name="文章标题")
    category = models.ForeignKey(PlateCategory, on_delete=models.CASCADE, verbose_name="所属分类")
    # 文章栏目（一对多）
    column = models.ForeignKey(PostColumn, null=True, blank=True, on_delete=models.CASCADE, related_name="post",
                               verbose_name="文章栏目")
    author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="持有者")
    # 文章标签
    tags = TaggableManager(blank=True)
    #    name = models.CharField(max_length=40, verbose_name="名称")
    #    desc = models.CharField(max_length=200, blank=True, null=True, verbose_name="文章摘要")
    likeUser=models.ManyToManyField(User,related_name="postlikeuser",verbose_name="点赞人名称")
    content = RichTextUploadingField(verbose_name="文章内容")
    #    image = models.ImageField(null=True, verbose_name="图片")
    status = models.PositiveIntegerField(default=STATUS_NORMAL, choices=STATUS_ITEMS, verbose_name="状态")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    no_visitor = models.PositiveIntegerField(default=0, verbose_name="访问人数")
    no_like = models.PositiveIntegerField(default=0, verbose_name="点赞人数")
    no_comment = models.PositiveIntegerField(default=0, verbose_name="评论人数")
    no_enshrine = models.PositiveIntegerField(default=0, verbose_name="收藏人数")

    def __str__(self):
        return self.content

    class Meta:
        verbose_name = verbose_name_plural = "板块文章"
        ordering = ['-created_time']

    @classmethod
    def get_posts(cls):
        return cls.objects.filter(status=cls.STATUS_NORMAL).order_by('-created_time')

    # 获取文章地址
    def get_absolute_url(self):
        return reverse('communications:postDetail', args=[self.id])


class PlateComment(models.Model):
    """
    分类下所属的文章的评论
    """
    STATUS_NORMAL = 1
    STATUS_HIDDEN = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, "正常"),
        (STATUS_HIDDEN, "隐藏")
    )
    #    name = models.CharField(max_length=40, unique=True, verbose_name="评论名称")
    post = models.ForeignKey(PlatePost, on_delete=models.CASCADE, verbose_name="所属文章")
    author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="持有者")
    #    name = models.CharField(max_length=40, verbose_name="名称")
    #    desc = models.CharField(max_length=200, blank=True, null=True, verbose_name="文章摘要")
    content = models.TextField(verbose_name="评论内容")
    status = models.PositiveIntegerField(default=STATUS_NORMAL, choices=STATUS_ITEMS, verbose_name="状态")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    no_like = models.PositiveIntegerField(default=0, verbose_name="点赞人数")

    def __str__(self):
        return self.content

    class Meta:
        verbose_name = verbose_name_plural = "文章评论"
        ordering = ['-created_time']

    @classmethod
    def get_posts(cls):
        return cls.objects.filter(status=cls.STATUS_NORMAL).order_by('-created_time')


class PostLike(models.Model):
    """
    分类下所属的文章的点赞
    """
    STATUS_NORMAL = 1
    STATUS_HIDDEN = 0
    STATUS_ITEMS = (
        (STATUS_NORMAL, "正常"),
        (STATUS_HIDDEN, "隐藏")
    )
    post = models.ForeignKey(PlatePost, on_delete=models.CASCADE, verbose_name="所属文章")
    author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="持有者")
    created_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = verbose_name_plural = "点赞"
        ordering = ['-created_time']
